package com.szss.springbootfirst.service.impl;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.szss.springbootfirst.entity.User;
import com.szss.springbootfirst.mapper.UserMapper;
import com.szss.springbootfirst.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author wcx
 * @date 2023/2/1 16:52
 */

@Service
public class UserServiceImpl implements IUserService {

    @Autowired
    private UserMapper userMapper;

    @Resource
    private RedisTemplate redisTemplate;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public User login(User user) {
        return userMapper.login(user);
    }

    @Override
    public String findDataFormRedis() throws JsonProcessingException {

        ValueOperations ops = redisTemplate.opsForValue();
        String resultList = (String) ops.get("user::findAll");
        if (resultList == null || resultList.trim().length() == 0){
            System.out.println("Redis中没有数据！");
            List<User> list = userMapper.findAll();
            ObjectMapper objectMapper = new ObjectMapper();
            resultList = objectMapper.writeValueAsString(list);
            ops.set("user::findAll",resultList);
        }
        return resultList;
    }

    @Override
    public void save(User user) throws JsonProcessingException {
        ValueOperations<String, String> ops = stringRedisTemplate.opsForValue();
        ObjectMapper objectMapper = new ObjectMapper();
        String stringUser = objectMapper.writeValueAsString(user);
        ops.set(user.getId(),stringUser);
        userMapper.save(user);
        System.out.println("用户保存成功！");
    }

    @Override
    public String findById(String id) throws JsonProcessingException {
        ValueOperations<String, String> ops = stringRedisTemplate.opsForValue();
        String resultUser = ops.get(id);
        if (resultUser == null || resultUser.trim().length() == 0){
            System.out.println("没查到id为"+id+"此用户！");
            User userById = userMapper.findById(id);
            if (userById != null){
                ObjectMapper objectMapper = new ObjectMapper();
                String resultStringUserById = objectMapper.writeValueAsString(userById);
                ops.set(id,resultStringUserById);
            }

        }
        return resultUser;
    }

//    @Cacheable(value = "user",key = "'findAll222'",unless = "#result == null ")
    @Override
    public List<User> findAll() {
        return userMapper.findAll();
    }

    @CacheEvict(value = "user",key = "'findAll222'")
    @Override
    public User save2(User user) {
        userMapper.save(user);
        return user;
    }
}
